package com.example.moviecms2.controller;

import com.example.moviecms2.domain.Actor;
import com.example.moviecms2.domain.Movie;
import com.example.moviecms2.domain.Role;
import com.example.moviecms2.repository.ActorRepository;
import com.example.moviecms2.repository.MovieRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/movie")
public class MovieController {

    @Autowired
    private MovieRepository movieRepository;

    @Autowired
    private ActorRepository actorRepository;

    @RequestMapping(value = "/detail/{id}")
    public String detail(Model model, @PathVariable Long id){
        Movie movie = movieRepository.findOne(id);
        model.addAttribute("info", movie);

        for (Role role: movie.getRoles()) {
            System.out.println(role.getName());

        }
        return "movie/detail";
    }
    @RequestMapping(value = {"","/","/index2"})
    public String index2(Model model){
        List<Movie> list = movieRepository.findAll();
        model.addAttribute("list",list);
        return "movie/index2";
    }

    @RequestMapping(value = {"","/","/index"})
    public String index(Model model){
        List<Movie> list = movieRepository.findAll();
        model.addAttribute("list",list);
        return "movie/index";
    }


    //movie/add  ,跳转到Add.html页面
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String toAdd(){
        return "movie/add";
    }

    //执行保存操作，然后重定向到列表页面 index.html
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String doAdd(@RequestParam String name,@RequestParam String photo,@RequestParam String createDate){
        //重定向（浏览器），请求转发（服务器）
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = format.parse(createDate);
        } catch (Exception e) {
        }


        System.out.println("name = "+name);
        Movie movie = new Movie();
        movie.setName(name);
        movie.setPhoto(photo);
        movie.setCreateDate(date);
        movieRepository.save(movie);
        return "redirect:/movie/index";
    }
    //toEdit()方法跳转到编辑页面
    @RequestMapping(value = "/edit/{id}",method = RequestMethod.GET)
    public String toEdit(Model model, @PathVariable Long id){
        Movie movie = movieRepository.findOne(id);
        model.addAttribute("info",movie);
        return "movie/edit";
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String doEdit(@RequestParam  Long id,@RequestParam  String name,@RequestParam  String photo,@RequestParam String createDate){
        Movie movie = movieRepository.findOne(id);
        SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try{
            date = format.parse(createDate);
        }catch (Exception e){

        }
        movie.setName(name);
        movie.setPhoto(photo);
        movie.setCreateDate(date);
        movieRepository.save(movie);
        return "redirect:/movie/index" ;
    }

    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public String delete(@PathVariable Long id){
        movieRepository.delete(id);
        return "redirect:/movie/index";
    }

}
